package jbl.service.impl;

import java.util.ArrayList;
import java.util.List;

import jbl.dao.GsDaoI;
import jbl.model.Code_gs;
import jbl.pageModel.DataGrid;
import jbl.pageModel.Gs;
import jbl.service.GsServiceI;
import jbl.util.Encrypt;
import jbl.util.Json;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
 * 用户权限菜单表
 * */
@Service("gsService")
public class GsServiceImpl implements GsServiceI{
	
	private static final Logger logger = Logger.getLogger(GsServiceImpl.class);

	
	private GsDaoI gsDao;
	
	@Autowired
	public void setGsDao(GsDaoI gsDao) {
		this.gsDao = gsDao;
	}


	@Override
	public DataGrid dataGrid() {
		DataGrid dg = new DataGrid();
		try {
			List<Gs> rl = new ArrayList<Gs>();
			String hql = "from Code_gs t ";
			List<Code_gs> nl = gsDao.find(hql);
			if(null != nl && nl.size() > 0){
				for(Code_gs t : nl){
					Gs gs = new Gs();
					BeanUtils.copyProperties(t, gs);
					rl.add(gs);
				}
				dg.setTotal((long)rl.size());
				dg.setRows(rl);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询Gs表错误:"+e.getMessage());
		}
		return dg;
	}
	
	@Override
	public Json add(Gs gs){
		Json j = new Json();
		try {
			Code_gs code_gs = new Code_gs();
			
			String userid = Encrypt.createUUID();
			code_gs.setId(userid);
			code_gs.setGs(gs.getGs());

			gsDao.save(code_gs);
			j.setMsg("添加成功");
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setMsg("添加失败");
		}
		return j;
	}
	
	@Override
	public Json edit(Gs gs){
		Json j = new Json();
		try {
			Code_gs code_gs = new Code_gs();
			BeanUtils.copyProperties(gs, code_gs);
			gsDao.update(code_gs);
			j.setMsg("修改成功");
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setMsg("修改失败");
		}
		return j;
	}
	

	@Override
	public Json delete(String  ids) {
		Json j = new Json();
		try {
			if(ids != null){
				for (String id : ids.split(",")) {
					if(!id.trim().equals("")){
						Code_gs t = gsDao.get(Code_gs.class,id.trim());
						gsDao.delete(t);
					}
				}
			}
			j.setMsg("删除成功");
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg("删除失败");
		}
		return j;
	}
	@Override
	public List<Gs> comboboxInfo() {
		List<Gs> rl = new ArrayList<Gs>();
		try {
			
			String hql = "from Code_gs t ";
			List<Code_gs> nl  = gsDao.find(hql);
			if(null != nl && nl.size() > 0){
				for(Code_gs t : nl){
					Gs gs = new Gs();
					BeanUtils.copyProperties(t, gs);
					rl.add(gs);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询Gs表错误:"+e.getMessage());
		}
		return rl;
	}

}











